package com.nsoft.thickclient.trackMove.search;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import com.nsoft.thickclient.beanUtils.ActionDescriptor;
import com.nsoft.thickclient.beanUtils.NButton;
import com.nsoft.thickclient.controller.ScreenView;
import com.nsoft.thickclient.table.NTable;
import com.nsoft.thickclient.table.VoPropertyTableModel;

public class SearchView<S> extends ScreenView<S> {

	/**
	 * serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private NTable table;

	private ScreenView<S> view;

	public SearchView(List<S> data, String[] properties, String[] headers,
			ScreenView<S> view, SearchViewController controller) {

		this.view = view;
		this.view
				.setBorder(BorderFactory.createTitledBorder("Search criteria"));
		table = new NTable(data, properties, headers);
		table.addTableRowSelectionListener(controller);
		table.getSelectionModel().setSelectionMode(
				ListSelectionModel.SINGLE_SELECTION);
		setLayout(new BorderLayout());
		JScrollPane tablePane = new JScrollPane(table);
		tablePane.getViewport().setPreferredSize(new Dimension(200, 400));
		add(tablePane, BorderLayout.WEST);
		JPanel criteriaPanel = new JPanel(new BorderLayout());
		criteriaPanel.add(getButtonPanel(controller), BorderLayout.SOUTH);
		criteriaPanel.add(new JScrollPane(this.view), BorderLayout.NORTH);
		add(criteriaPanel, BorderLayout.CENTER);

	}

	private JComponent getButtonPanel(SearchViewController controller) {
		JPanel butonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		butonPanel.add(new NButton(new ActionDescriptor("Search", "doSearch",
				controller)));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(new ActionDescriptor("Edit", "doEdit",
				controller)));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(new ActionDescriptor("Delete", "doDelete",
				controller)));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(new ActionDescriptor("Clear", "doClear",
				controller)));
		/*butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(new ActionDescriptor("Cancel", "doCancel",
				controller)));*/
		return butonPanel;
	}

	public S getWorkingBean() {
		return view.getWorkingBean();
	}

	public void setWorkingBean(S bean) {
		view.setWorkingBean(bean);

	}

	public void setTableData(List<S> data) {
		if (data != null) {
			((VoPropertyTableModel) table.getModel()).setData(data);
			table.revalidate();
		}
	}

	S getSelectedBean() {
		if (table.getSelectedRow() > -1) {
			return (S) ((VoPropertyTableModel) table.getModel())
					.getBeanForRow(table.getSelectedRow());
		}
		return null;
	}

}
